import api from '@/api';

export default {
  namespace: 'notice',

  state: {
    notices: {
      list: [],
      pagination: {},
    },
    cities: [],
    status: [],
    targets: [],
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(api.notice.query, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveNotices',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },
    *delete({ payload }, { call }) {
      return yield call(api.notice.delete, payload);
    },
    *create({ payload }, { call }) {
      return yield call(api.notice.create, payload);
    },
    *update({ payload }, { call }) {
      return yield call(api.notice.update, payload);
    },
    *submitAudit({ payload }, { call }) {
      return yield call(api.notice.submitAudit, payload);
    },
    *audit({ payload }, { call }) {
      return yield call(api.notice.audit, payload);
    },
    *stop({ payload }, { call }) {
      return yield call(api.notice.stop, payload);
    },
    *fetchCities(_, { call, put }) {
      let res = yield call(api.notice.cities);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCities',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchStatus(_, { call, put }) {
      let res = yield call(api.notice.status);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveStatus',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchTargets({ payload }, { call, put }) {
      let res = yield call(api.notice.target, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveTargets',
          payload: res.data,
        });
      }
      return res;
    },
  },

  reducers: {
    saveNotices(state, { payload }) {
      return {
        ...state,
        notices: payload,
      };
    },
    saveCities(state, { payload }) {
      return {
        ...state,
        cities: payload,
      };
    },
    saveStatus(state, { payload }) {
      return {
        ...state,
        status: payload,
      };
    },
    saveTargets(state, { payload }) {
      return {
        ...state,
        targets: payload,
      };
    },
  },
};
